package com.noble.glamour.module.util;

import android.text.TextUtils;

import com.noble.library.dir.DirType;
import com.noble.library.log.PSP;
import com.noble.library.request.DownloadListener;
import com.noble.library.utils.FileUtil;
import com.noble.library.host.Hosts;
import com.noble.glamour.module.logic.model.impl.HttpMgrImpl;
import com.noble.glamour.module.logic.model.mgr.HttpMgr;

import java.util.HashMap;

/**
 * 下载H5到本地
 *
 * @author guowz
 */
public class WebAppDownloader {
    private static WebAppDownloader instance;

    // 应用内H5文件
    public static final String KEY_WEB_APP_ROOT = "sp_key_webapp_root";
    private static final String KEY_WEB_APP_VER = "sp_key_webapp_ver";
    private HashMap<String, String> downMap = new HashMap<>();

    // 音视频H5文件
    public static final String KEY_WEB_VIDEO_ROOT = "sp_key_webvideo_root";
    private static final String KEY_WEB_VIDEO_VER = "sp_key_webvideo_ver";
    private HashMap<String, String> videoMap = new HashMap<>();

    private WebAppDownloader() {
    }

    public static WebAppDownloader getInstance() {
        if (instance == null) {
            synchronized (WebAppDownloader.class) {
                if (instance == null) {
                    instance = new WebAppDownloader();
                }
            }
        }
        return instance;
    }

    public void download(String url, final long ver) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (downMap.containsKey(url) || (PSP.getInstance().getLong(KEY_WEB_APP_VER, 0) >= ver && FileUtil.isExist(PSP.getInstance().getString(KEY_WEB_APP_ROOT, ""))))
            return;
        downMap.put(url, url);
        HttpMgr httpMgr = new HttpMgrImpl();
        String zipFile = DirType.getCacheDir() + ver + FileUtil.getFileNameFromUrl(url);
        httpMgr.download(url + "?v=" + ver, zipFile, new DownloadListener() {
            @Override
            public void onStart(String url, String filePath) {
            }

            @Override
            public void onProcess(String url, int process, long size) {
            }

            @Override
            public void onSuccess(String url, String filePath) {
                downMap.clear();
                String outputPath = DirType.getWebDir() + "yfb";
                String path = FileUtil.unZipFile(filePath, outputPath);
                PSP.getInstance().put(KEY_WEB_APP_ROOT, path);
                PSP.getInstance().put(KEY_WEB_APP_VER, ver);
                Hosts.initWebAppUrl();
            }

            @Override
            public void onFail(String url, Throwable throwable) {
                downMap.clear();
            }
        });
    }

    /**
     * 音视频h5下载
     * @param url
     * @param ver
     */
    public void downloadVideoH5(String url, final long ver) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (videoMap.containsKey(url) || (PSP.getInstance().getLong(KEY_WEB_VIDEO_VER, 0) >= ver && FileUtil.isExist(PSP.getInstance().getString(KEY_WEB_VIDEO_ROOT, ""))))
            return;
        videoMap.put(url, url);
        HttpMgr httpMgr = new HttpMgrImpl();
        String zipFile = DirType.getCacheDir() + ver + FileUtil.getFileNameFromUrl(url);
        httpMgr.download(url + "?v=" + ver, zipFile, new DownloadListener() {
            @Override
            public void onStart(String url, String filePath) {
            }

            @Override
            public void onProcess(String url, int process, long size) {
            }

            @Override
            public void onSuccess(String url, String filePath) {
                videoMap.remove(url);
                String outputPath = DirType.getWebDir();
                String path = FileUtil.unZipFile(filePath, outputPath);
                PSP.getInstance().put(KEY_WEB_VIDEO_ROOT, path);
                PSP.getInstance().put(KEY_WEB_VIDEO_VER, ver);
            }

            @Override
            public void onFail(String url, Throwable throwable) {
                videoMap.remove(url);
            }
        });
    }
}
